package com.archgeek.calcite;

import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.util.ConversionUtil;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyAgreementSpi.DHwithSHA1KDF;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author pizhihui
 * @date 2024-05-30 23:09
 */
public class InMemoryRunner {


    public static void main(String[] args) {

        System.setProperty("saffron.default.charset", ConversionUtil.NATIVE_UTF16_CHARSET_NAME);
        System.setProperty("saffron.default.nationalcharset",ConversionUtil.NATIVE_UTF16_CHARSET_NAME);
        System.setProperty("saffron.default.collation.name", ConversionUtil.NATIVE_UTF16_CHARSET_NAME + "$en_US");


        try {
            Class.forName("org.apache.calcite.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        Properties info = new Properties();

        String modelPath = "/Users/pizhihui/ProjectsIdea/code-bigdata/code-calcite/src/main/resources/bookshop.json";

        try {
            Connection connection = DriverManager.getConnection("jdbc:calcite:model=" + modelPath, info);

            CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);

//            ResultSet result = connection.getMetaData().getTables(null, null, null, null);
//            while (result.next()) {
//
//                System.out.println("catalog: " + result.getString(1) + ",database:" + result.getString(2) + ",table:" + result.getString(3));
//            }
//
//            result.close();

            Statement st = connection.createStatement();
            ResultSet result = st.executeQuery("select * from book as b where b.name = '数据山'");




            while(result.next()) {
                // org.apache.calcite.jdbc.CalciteResultSet
                System.out.println("result===" + result);
                System.out.println(result.getString(1) + "\t" + result.getString(2) + "\t" + result.getString(3));
//                System.out.println(result.getString(1) + "\t" );
            }
            result.close();

            //connection.close();
//            st = connection.createStatement();
//            result = st.executeQuery("select a.name from author as a");
//            while(result.next()) {
//                System.out.println(result.getString(1));
//            }
//            result.close();

            connection.close();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }

    }
}
